/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PregledServisaPanel.java
 *
 * Created on 19.01.2011., 19:17:39
 */

package hraplication;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Fedja
 */
public class PregledZahtjevaSPadminPanel extends Paneli {
    Konekcija kon;
    List<Integer> uposlenici;
    /** Creates new form PregledServisaPanel */
    public PregledZahtjevaSPadminPanel(Konekcija konekcija) {
        initComponents();
        kon=konekcija;
        uposlenici = new ArrayList<Integer>(100);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jComboBox1 = new javax.swing.JComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        tabela = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();

        setName("Form"); // NOI18N

        jComboBox1.setName("jComboBox1"); // NOI18N
        jComboBox1.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                jComboBox1PropertyChange(evt);
            }
        });

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        tabela.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "ID", "Datum pocetka", "Datum zavrsetka", "Odrediste", "Akontacija", "Prevozno Sredstvo", "Status"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.Float.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tabela.setName("tabela"); // NOI18N
        tabela.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tabelaMouseClicked(evt);
            }
        });
        tabela.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                tabelaKeyReleased(evt);
            }
        });
        jScrollPane1.setViewportView(tabela);

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(hraplication.HRaplicationApp.class).getContext().getResourceMap(PregledZahtjevaSPadminPanel.class);
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 477, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 157, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(11, 11, 11)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 252, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jComboBox1PropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_jComboBox1PropertyChange
        OsvjeziTabelu();
    }//GEN-LAST:event_jComboBox1PropertyChange

    private void tabelaKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tabelaKeyReleased
        if (evt.getKeyCode() == evt.VK_DELETE){
            int oznaceni[] = tabela.getSelectedRows();
            if (oznaceni.length==0){
                JOptionPane.showMessageDialog(this, "Oznacite red(ove) koje zelite izbrisati!", "Info",WIDTH,null);
                return;
            }
            DefaultTableModel m = (DefaultTableModel) tabela.getModel();

            String kom = "DELETE FROM zahtjevi_za_sp WHERE idsp="+m.getValueAt(oznaceni[0], 0);
            for (int i=1; i<oznaceni.length; i++){
                kom = kom + " OR idsp="+m.getValueAt(oznaceni[i], 0);
            }
            String greska = kon.Delete(kom);
            if (greska != null)
                //DOSLO DO GRESKE
                JOptionPane.showMessageDialog(this, greska, "Greska",WIDTH,null);

            OsvjeziTabelu();
            tabela.clearSelection();

        }
        if (evt.getKeyCode() == evt.VK_ESCAPE){
            tabela.clearSelection();
        }
    }//GEN-LAST:event_tabelaKeyReleased

    private void tabelaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tabelaMouseClicked
        if (evt.getClickCount()==2){
            //UKOLIKO JE STATUS VEC ODOBREN
            if (tabela.getSelectedRowCount()==1){
                if (tabela.getValueAt(tabela.getSelectedRow(),6).equals("ODOBREN")){
                    JOptionPane.showMessageDialog(this, "Zahtjev je vec odobren", "Info",JOptionPane.INFORMATION_MESSAGE);
                }
                else{
                    int ret = JOptionPane.showConfirmDialog(this,"Da li zelite odobriti ovaj zahtjev","Odobravanje zahtjeva",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);
                    if (ret==JOptionPane.OK_OPTION){
                        //UPDATE REDA
                        String upd = "UPDATE zahtjevi_za_sp SET status_zahtjeva = 'ODOBREN' WHERE idsp = "+
                                tabela.getValueAt(tabela.getSelectedRow(),0);
                        try {
                            String greska = kon.Insert(upd);
                            if (greska != null)
                                //DOSLO DO GRESKE
                                JOptionPane.showMessageDialog(this, greska, "Greska",JOptionPane.ERROR_MESSAGE);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        OsvjeziTabelu();
                    }
                }
            }
        }
    }//GEN-LAST:event_tabelaMouseClicked

    public void OsvjeziChBOX(){
        //Popunjavanje prvog comboboxa
        uposlenici.clear();
        jComboBox1.removeAllItems();
        ResultSet r = kon.Select("SELECT idUposlenici, Ime, Prezime FROM uposlenici");
        if (r!=null){
            try{
                while (r.next()){
                    uposlenici.add(r.getInt(1));
                    String dd = r.getString(2)+" "+r.getString(3);
                    jComboBox1.addItem(dd);
                }
            }
            catch (SQLException ex)
            {
              System.err.println(ex.getMessage());
            }
        }
        OsvjeziTabelu();

        
    }
    public void OsvjeziTabelu(){
        //Popunjavanje tabele
        DefaultTableModel m = (DefaultTableModel) tabela.getModel();
        m.getDataVector().removeAllElements();
        
        if (jComboBox1.getSelectedIndex()>=0){
            String upit = "SELECT z.idsp, z.Datum_pocetka, z.Datum_zavrsetka, z.odrediste, z.akontacija, z.prevoznosredstvo, z.status_zahtjeva FROM zahtjevi_za_sp AS z, uposlenici AS u WHERE z.Uposlenici_iduposlenici = u.iduposlenici AND u.iduposlenici = ";
            upit = upit + uposlenici.get(jComboBox1.getSelectedIndex());

            ResultSet r = kon.Select(upit);
            if (r!=null){
                try{
                    while (r.next()){
                        String prevoz;
                        if (r.getDouble(6)==0)
                            prevoz = "Sluzbeno vozilo";
                        else
                            prevoz = "Privatno vozilo";
                        m.addRow(new Object[] {r.getInt(1), r.getDate(2), r.getDate(3), r.getString(4), r.getFloat(5), prevoz, r.getString(7)});
                    }
                }
                catch (SQLException ex)
                {
                  System.err.println(ex.getMessage());
                }
            }
        }
        tabela.repaint();

    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tabela;
    // End of variables declaration//GEN-END:variables

}
